package com.xgq.drink.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xgq.drink.config.AliPayConfig;
import com.xgq.drink.entity.Payment;
import com.xgq.drink.mapper.PaymentMapper;
import com.xgq.drink.service.IOrderService;
import com.xgq.drink.service.IPaymentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgq.drink.utils.UniqueIdGenerator;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xgq
 * @since 2025-03-04
 */
@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, Payment> implements IPaymentService {
    @Autowired
    private PaymentMapper paymentMapper;
    @Autowired
    private AliPayConfig aliPayConfig;
    private static final String GATEWAY_URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    private static final String FORMAT = "JSON";
    private static final String CHARSET = "utf-8";
    private static final String SIGN_TYPE = "RSA2";

    public Payment savePayment(Payment payment) {
        paymentMapper.insert(payment);
        return payment;
    }
    public Payment getPaymentByOrderId(Long orderId) {
        return paymentMapper.selectOne(new QueryWrapper<Payment>().eq("order_id", orderId));
    }
    public boolean isNotifyProcessed(String notifyId) {
        QueryWrapper<Payment> query = new QueryWrapper<>();
        query.eq("alipay_notify_id", notifyId);
        return this.count(query) > 0;
    }
    public void updatePayment(Payment payment) {
        paymentMapper.updateById(payment);
    }
    public IPage<Payment> getPaymentList(int current, int size) {
        // 创建分页对象
        Page<Payment> page = new Page<>(current, size);

        // 查询分页数据
        IPage<Payment> paymentPage = paymentMapper.selectPage(page, null);

        // 返回分页结果
        return paymentPage;
    }
    //退钱
    public Payment getPaymentByVoucher(String voucher) {
        QueryWrapper<Payment> wrapper = new QueryWrapper<>();
        wrapper.eq("voucher", voucher);
        return getOne(wrapper);
    }
    @Override
    public boolean refund(String tradeNo, BigDecimal refundAmount) throws AlipayApiException {
        AlipayClient alipayClient = new DefaultAlipayClient(
                GATEWAY_URL,
                aliPayConfig.getAppId(),
                aliPayConfig.getAppPrivateKey(),
                FORMAT,
                CHARSET,
                aliPayConfig.getAlipayPublicKey(),
                SIGN_TYPE);

        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        request.setBizContent("{" +
                "\"trade_no\":\"" + tradeNo + "\"," +
                "\"refund_amount\":\"" + refundAmount + "\"," +
                "\"out_request_no\":\"" + UniqueIdGenerator.generateUniqueId() + "\"" +
                "}");

        // 执行退款请求
        return alipayClient.execute(request).isSuccess();
    }
}
